using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.IO;

namespace Game
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        CharacterManager characterManager;
        MapManager mapManager;
        int stateGame=1;// che do 1 la game, 2 la battle , 0 la menu


        public static int time;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            Window.Title="One Piece";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        private int _ncharacter;
        private List<Character> _character;

        private int _nboss;
        private List<Character> _boss;

        private int _nmap;
        private List<Map> _map;

        public static int desX = 0;
        public static int desY = 0;

        private int _nbackGround;
        private List<Map> _backGround;

        MouseState mouseinfo;
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            //------------------- doc map------------------------
            FileStream myFStream = new FileStream("map.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);

            myFStream.Position = 0;
            int row = -1;
            int col = -1;
            for (int i = 0; i < 3; i++)
            {
                int temp = myFStream.ReadByte() - 48;
                if (temp > 0)
                {
                    if (row == -1)
                        row = temp;
                    else col = temp;
                }
            }
            int[] matrix = new int[row * col];
            myFStream.Position = 5;
            //int temp = 0;
            int count = 0;
            for (int i = 0; i < myFStream.Length - 4; i++)
            {
                int temp = myFStream.ReadByte() - 48;
                if (temp > 0)
                {
                    matrix[count] = temp;
                    count++;
                }
            }
            myFStream.Close();
            //------------------------------------------------------
            characterManager = new CharacterManager();
            characterManager.InitPrototypes(this.Content);

           // mapManager = new MapManager();
           // mapManager.InitPrototypes(this.Content);


            _ncharacter = 1;
            _character= new List<Character>();

            _character.Add((Character)characterManager.CreateObject(0));
            _character[0].X = 100;
            _character[0].Y = 100;

            _nboss = 1;
            _boss = new List<Character>();
            _boss.Add((Character)characterManager.CreateObject(1));
            _boss[0].X = 824;
            _boss[0].Y = 550;


            mapManager = new MapManager();
            mapManager.InitPrototypes(this.Content);

            _nmap = row * col;
            _map = new List<Map>();


            for (int i = 0; i < row * col; i++)
            {
                _map.Add((Map)mapManager.CreateObject(matrix[i]));
                _map[i].X = (float)1024 * (i % row);
                _map[i].Y = (float)768 * (i / row);
            }

            _nbackGround = 1;
            _backGround = new List<Map>();
            _backGround.Add((Map)mapManager.CreateObject(7));
            _backGround[0].X = 0.0f;
            _backGround[0].Y = 0.0f;
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            IsMouseVisible = true;
            mouseinfo = Mouse.GetState();
            KeyboardState state = Keyboard.GetState();

            switch(stateGame)
            {
                case 1:
                    {
                        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                            this.Exit();

                        for (int i = 0; i < _ncharacter; i++)
                            _character[i].Update(gameTime);
                        // TODO: Add your update logic here

                        //for (int i = 0; i < _ncharacter; i++)
                        //    _character[i].updatekey(gameTime, 0);

                        for (int i = 0; i < _nmap; i++)
                            _map[i].Update(gameTime);

                        for (int i = 0; i < _nmap; i++)
                            _map[i].updatekey(gameTime, 0);


                        if (mouseinfo.LeftButton == ButtonState.Pressed)
                        {
                            desX = mouseinfo.X;
                            desY = mouseinfo.Y;

                        }
                        if (desX != 0 && desY != 0)
                        {
                            bool statechar = false;
                            for (int i = 0; i < _ncharacter; i++)
                                statechar = _character[i].updateMouse(gameTime, 0, desX, desY);

                            for (int i = 0; i < _ncharacter; i++)
                                _character[i].updatekey(gameTime, 0, desX, desY);
                        }
                        else
                        {
                            for (int i = 0; i < _ncharacter; i++)
                                _character[i].updatekey(gameTime, 0);
                        }

                        // chuyen che do game
                        if (state.IsKeyDown(Keys.Enter))
                            stateGame = 2;

                        break;
                    }
                case 2:
                    {
                        for(int i=0; i<_ncharacter; i++)
                        {
                            _character[i].X = 0 * (i+1);
                            _character[i].Y = 500 * (i+1);
                        }
                        for (int i = 0; i < _ncharacter; i++)
                            _character[i].Update(gameTime, 0, "run");
                        for (int i = 0; i < _nboss; i++)
                            _boss[i].Update(gameTime);
                        break;
                    }
        }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {

            GraphicsDevice.Clear(Color.CornflowerBlue);
            
            spriteBatch.Begin();
            switch (stateGame)
            {
                case 1:
                    {
                        for (int i = 0; i < _nmap; i++)
                            _map[i].Draw(gameTime, spriteBatch);

                        for (int i = 0; i < _ncharacter; i++)
                            _character[i].Draw(gameTime, spriteBatch);
                        break;
                    }
                case 2:
                    {
                        _backGround[0].Draw(gameTime, spriteBatch);
                        for (int i = 0; i < _ncharacter; i++)
                            _character[i].Draw(gameTime, spriteBatch);
                        for (int i = 0; i < _nboss; i++)
                            _boss[i].Draw(gameTime, spriteBatch);
                        break;
                    }
            }
            spriteBatch.End();
            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
